Every SQL RDBMS follow a similar set of rules and syntax which are known as queries and statements. While writing the queries in SQL we have to take care of syntax, if we write a query with invalid syntax then the SQL throw some errors. Every SQL query or statement starts with a command like SELECT, INSERT, UPDATE, DELETE, ALTER, DROP, CREATE, USE, SHOW, etc. and ends with a semicolon (;), if we do not put a semicolon the query will not execute. SQL commands are insensitive in nature which mean SELECT and select have the same meaning, and this case-insensitive concept also applies on the database and tables, if there is a Database with name Music then you cannot create another database with name MusiC, or music, or MuSic.
SQL Syntax for various commands
1. SQL SELECT Command
SELECT Field_name_1, Field_name_2 FROM table_name; here SELECT and FROM are SQL commands.
2. SQL DISTINCT Clause:
SELECT DISTINCT Field_name_1, Field_name_2 FROM table_name;
3. SQL WHERE Statement:
SELECT Field_name_1, Field_name_2 FROM table_name WHERE Conditoin1 operator Condition2;
4. SQL AND/OR Operator
SELECT Field_name_1, Field_name_2 FROM table_name WHERE CONDITION-1 {AND|OR} CONDITION-2;
5. SQL IN Operator
SELECT Field_name_1, Field_name_2 FROM table_name WHERE Field_name IN (val1, val2, val3,…);
6. SQL BETWEEN Statement
SELECT Field_name_1, Field_name_2 FROM table_name WHERE Field_name BETWEEN val1 AND val2;
7. SQL LIKE Statement
SELECT Field_name_1, Field_name_2 FROM table_name WHERE Field_name LIKE { like_patterns };
8. SQL ORDER BY Statement:
SELECT Field_name_1, Field_name_2 FROM table_name WHERE CONDITION ORDER BY Field_name {ASC|DESC};
9. SQL GROUP BY Statement:
SELECT AVG(Field_name_1) FROM table_name WHERE CONDITION GROUP BY Field_name;
10. SQL COUNT Function:
SELECT COUNT(Field_name) FROM table_name WHERE condition;
11. SQL HAVING Statement:
SELECT AVG(Field_Name) FROM table_name WHERE condition GROUP BY Field_name HAVING (condition);
12. SQL CREATE TABLE Statement
CREATE TABLE table_name( field_name_1 datatype, field_name_2 datatype, field_neme_3 datatype, ..... field_name_N datatype, );
13. SQL DROP TABLE Statement
DROP TABLE table_name;
14. SQL CREATE INDEX Statement
CREATE UNIQUE INDEX index_name ON table_name ( Field_name_1, Field_name_2, ……. field_name_n);
15. SQL DROP INDEX Statement
ALTER TABLE table_name DROP INDEX index_name;
16. SQL DESC Statement
DESC table_name;
17. SQL TRUNCATE TABLE Statement
TRUNCATE TABLE table_name;
18. SQL ALTER TABLE Statement
ALTER TABLE table_name {ADD|DROP|MODIFY} Field_name {data_ype};
19. SQL Rename TABLE Syntax:
ALTER TABLE table_name RENAME TO new_table_name;
20. SQL INSERT INTO Statement
INSERT INTO table_name( Field_name_1, Field_name_2,…..Field_name_n) VALUES ( val1, val2....valN);
21. SQL UPDATE Statement
UPDATE table_name SET column1 = value1, column2 = value2....columnN=valueN [ WHERE CONDITION ];
22. SQL DELETE Statement
DELETE FROM table_name WHERE {condition};
23. SQL CREATE DATABASE Statement
CREATE DATABASE database_name;
24. SQL DROP DATABASE Statement
DROP DATABASE database_name;
25. SQL USE Statement
USE database_name;
26. SQL COMMIT Statement
COMMIT;
27. SQL ROLLBACK Statement
ROLLBACK;
SQL Syntax Quick Summary
- SQL statements are known as SQL queries.
- Each SQL query contains some SQL commands.
- SQL commands are Case insensitive.
- Every SQL query ends with a semicolon.
- If you pass an invalid SQL query then the SQL engine would throw an error.
People are also reading: